<?php

namespace Chap6Bundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Response;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

class BlogController extends Controller
{

	//调试所有的路由: php56 bin/console debug:router
	//调试指定的路由: php56 bin/console debug:router


	/**
	 * 生成绝对地址的URL
	 * @Route("genabsoluteurl")
	 */
	public function genAbsoluteURL()
	{
		//在后端生成绝对地址的URL
		$url = $this->generateUrl(
			'blog_show',
			array('slug' => 'my-blog-post'),
			UrlGeneratorInterface::ABSOLUTE_URL
		);

		var_dump($url);
		//string 'http://symfony.io:7889/app_dev.php/chap6/blogshow/my-blog-post' (length=62)

		//在模板中生成绝对地址的URL
		$html = "<a href=\"{{ url('blog_show', {'slug': 'my-blog-post'})}}\">";
		var_dump($html);
	}


	/**
	 * 在模板中生成url
	 * @Route("/genurlintmp")
	 * @return Response
	 */
	public function createURLintemplateAciton()
	{
		//在html代码中
		$html = "<a href=\"{{ path('blog_show', {'slug': 'all-about-posts'}) }}\">Read this blog post</a>";

		//在js代码中
		$html2 = "<a href=\"{{ path('blog_show', {'slug': 'my-blog-post'}) | escape('js') }}\">";

		return new Response($html);
	}

	/**
	 * @Route("/createurl")
	 */
	public function createUrlAction()
	{
		//匹配路由
		$params = $this->get("router")->match('/chap6/blogshow/my-blog-post');
		var_dump($params);
		/*
		array (size=3)
			'_controller' => string 'Chap6Bundle\Controller\BlogController::showAction' (length=49)
            'slug' => string 'my-blog-post' (length=12)
            '_route' => string 'blog_show' (length=9)
		*/


		//生成uri地址
		$uri = $this->get('router')->generate('blog_show', array('slug'=>'my-blog-post'));
		var_dump($uri);
		//string '/app_dev.php/chap6/blogshow/my-blog-post' (length=40)

		//生成url地址
		$url = $this->generateUrl('blog_show', array('slug'=>'all-about-symfony'));
		var_dump($url);
		//string '/app_dev.php/chap6/blogshow/all-about-symfony' (length=45)

		//生成带有参数的url地址
		$url2 = $this->get('router')->generate('blog_page', array(
			'page' => 2,
			'category' => 'symfony'
		));

		var_dump($url2);
		//string '/app_dev.php/chap6/blog/2?category=symfony' (length=42)
	}


	/**
	 * @Route("/api/posts/{id}")
	 * @Method("PUT")
	 * @param $id
	 */
	public function editAction($id)
	{

	}


	/**
	 * @Route("/api/posts/{id}")
	 * @Method({"GET", "HEAD"})
	 * @param $id
	 */
	public function apishowAction($id)
	{

	}

	/**
	 * @Route("/blogshow/{slug}", name="blog_show")
	 * @param $slug
	 */
	public function showAction($slug)
	{
		return new Response('Your slug: '.$slug);
	}


	/**
	 * @Route("/blog/{page}", defaults={"page"=1}, requirements={"page": "\d+"}, name="blog_page")
	 * @param $page
	 */
	public function indexAction($page)
	{

	}


	/**
	 * 默认参数
	 * @Route("/{_locale}", defaults={"_locale": "en"}, requirements={"_locale": "en|fr"})
	 * @param $_locale
	 */
	public function homepageAction($_locale)
	{

	}



}
